<template>
  <div class="arrearsStatistics-page">
    <breadcrumb />
    <div class="search-container">
      <Search @search="getSearchParams" />
    </div>
    <div class="table-container">
      <div class="table-component">
        <div class="submit-btns">
          <div></div>
          <div class="submit-btns-right">
            <el-button
              type="primary"
              class="submit-btn-export"
              @click="exportData"
            >
              <img class="icon-img" :src="iconExport" alt="" />
              导出</el-button
            >
          </div>
        </div>
        <Table
          :tableData="table.data"
          :loading="table.loading"
          @table="getTableParams"
        />
        <div class="pagination">
          <el-pagination
            v-model:current-page="pagination.currentPage"
            v-model:page-size="pagination.pageSize"
            :page-sizes="pagination.pageSizes"
            layout="total,sizes, prev, pager, next, jumper"
            :total="table.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
        <el-dialog
          v-model="dialogVisible"
          :title="title"
          destroy-on-close
          :before-close="handleClose"
          :close-on-click-modal="false"
          :width="1200"
        >
          <component
            :is="componentTag"
            @operationDone="operationDone"
            :operationId="operationId"
          >
          </component>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script setup>
import breadcrumb from "@/components/breadcrumb/index.vue";
import Search from "./components/search.vue";
import Table from "./components/table.vue";
import iconExport from "@/assets/image/btn/icon-export.png";
import detailsComponent from "./components/detailsComponent.vue";
import { reactive, ref, shallowRef } from "vue";
import {
  getArrearsList,
  getExportArrears,
} from "./request.js"

// 搜索
const searchModel = ref({
  keyword: "",
  startFee: "",
  endFee: "",
});
const getSearchParams = (value) => {
  // searchParams.value = value;
  searchModel.value = value;
  getData();
};

// 弹窗
const title = ref("");
const dialogVisible = ref(false);
const componentTag = shallowRef("");
const operationId = ref("");
const operationDone = (r) => {
  dialogVisible.value = false;
  operationId.value = "";
  if (!r) {
    getData();
  }
};

// 导出按钮
const exportData = () => {
  ElMessageBox.confirm(`您确认要导出数据？`, "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await getExportArrears({
      keyword: searchModel.value.keyword,
      startFee: searchModel.value.startFee,
      endFee: searchModel.value.endFee,
      orderBy: searchModel.value.orderBy,
      sort: searchModel.value.sort,
    })
      .then((res) => {
        const fileName = extractFileNameFromHeaders(res.headers);
        const url = window.URL.createObjectURL(new Blob([res.data]));
        const link = document.createElement("a");
        link.href = url;
        link.download = fileName;
        link.click();
      })
      .catch((err) => {
        console.log("err ===>", err);
      });
  });
};

function extractFileNameFromHeaders(headers) {
  const contentDisposition = headers.get("content-disposition") || "";

  const matches = contentDisposition.match(/filename\*=\s*utf-8''([^;]+)/);

  if (matches != null && matches[1]) {
    return decodeURIComponent(matches[1].replace('"', ""));
  }
  return "unknown-file.pdf"; // 默认文件名
}

// table操作
const getTableParams = (value) => {
  console.log("value ===>", value);
  if (value.operation === 1) {
    title.value = "欠费详情";
    componentTag.value = detailsComponent;
    operationId.value = value;
    dialogVisible.value = true;
  } else if (value.operation === 2) {
    searchModel.value.sort = value.sort;
    searchModel.value.orderBy = value.orderBy;
    getData();
  } else if (value.operation === 3) {
    searchModel.value.sort = value.sort;
    searchModel.value.orderBy = value.orderBy;
    getData();
  }
};

// 表格
const table = reactive({
  data: [],
  total: 0,
  loading: true,
});

const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  pageSizes: [10, 20, 30, 50],
});

const handleCurrentChange = (value) => {
  pagination.currentPage = value;
  getData();
};

const handleSizeChange = (value) => {
  pagination.pageSize = value;
  getData();
};

const getData = async () => {
  const res = await getArrearsList({
    keyword: searchModel.value.keyword,
    startFee: searchModel.value.startFee,
    endFee: searchModel.value.endFee,
    orderBy:searchModel.value.orderBy,
    sort: searchModel.value.sort,
    pageNo: pagination.currentPage,
    pageSize: pagination.pageSize,
  });
  if (res.data.code === 0) {
    table.data = res.data.result.items;
    table.total = parseInt(res.data.result.total);
    table.loading = false;
  }
};
getData();
</script>

<style lang="less" scoped>
.arrearsStatistics-page {
  .search-container {
    margin-top: 16px;
    min-height: 100%;
    padding: 24px 24px 8px 24px;
    background: #fff;
    border-radius: 6px;
  }
  .table-container {
    margin-top: 16px;
    min-height: 100%;
    padding: 24px;
    background: #fff;
    border-radius: 6px;
    .table-component {
      .submit-btns {
        display: flex;
        justify-content: space-between;
        margin-bottom: 24px;
        .submit-btns-right {
          display: flex;
        }
        .submit-btn-export {
          background: #35c362;
          border: 1px solid #35c362;
          border-radius: 2px;
          margin-left: 12px;
          padding: 5px 16px;
        }
        .icon-img {
          width: 16px;
          height: 16px;
          margin-right: 8px;
        }
      }
      .pagination {
        display: flex;
        align-items: center;
        justify-content: flex-end;
      }
    }
  }
}
</style>